/* <one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2011  Joseph Luccisano

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.lucciframework.validator.combo {
	
    import mx.controls.Alert;
    import mx.controls.ComboBox;
    import mx.core.IMXMLObject;
    import mx.events.ValidationResultEvent;
    import mx.validators.ValidationResult;
    
    import org.lucciframework.validator.core.AbstractValidator;
    import org.lucciframework.validator.error.LucciErrorValidator;
    import org.lucciframework.validator.error.LucciMessageCodes;
    
    import spark.components.ComboBox;
    import spark.components.List;
	
	/**
	 * The combo validator allows to check whether at least one element is selected
	 */

    public class LucciComboBoxValidator extends AbstractValidator implements IMXMLObject {


        private var _errorMessage : String = "Select one element";

		/**
		 * Get the error message which will display when the field is Invalid
		 */
        [Bindable] public function get errorMessage() : String {
            return _errorMessage;
        }

		/**
		 * Set the error message which will display when the field is Invalid
		 */
        public function set errorMessage(value : String) : void {
            _errorMessage = value;
        }

        /**
         * @private
         */
        override public function validate() : void {

            if (target is List) {

                (target).errorString = "";

                var array : Array = new Array();

                if (required) {
                    if (List(target).selectedItem != null) {
                        validationResultEvent = new ValidationResultEvent(ValidationResultEvent.VALID);
                    } else {

                        array.push(new ValidationResult(true, "", "requiredField", errorMessage));
                        validationResultEvent =
                            new ValidationResultEvent(ValidationResultEvent.INVALID, false, false, null, array);

                    }
                    validationResultEventArray = [ validationResultEvent ];
                }

            } else {
                throw new LucciErrorValidator(LucciMessageCodes.BAD_TARGET, target);
            }
        }

        /**
         * @private
         */
        public function initialized(document : Object, id : String) : void {}
    }
}